package UILayer;

import java.awt.*;
import javax.swing.*;
import CtrLayer.CustomerCtr;
import CtrLayer.LeaseCtr;
import javax.swing.table.*;
import java.awt.event.*;
import javax.swing.border.*;
import javax.swing.event.*;



public class LeasesGUI extends JInternalFrame
{
	private JTextField text_field;
	private LeaseCtr leases;
	private CustomerCtr customers;
	private JTable lease_table;
	private String[] column_names = { "Reg. Number", "Customer", "Active"};
	private JButton complete;
	


	public LeasesGUI()
	{
		super("Leases", true, true, true, true);
		this.addInternalFrameListener(new InternalFrameAdapter() { public void internalFrameActivated(InternalFrameEvent e) { refresh(); } });
		leases = new LeaseCtr();
		customers = new CustomerCtr();
		fill_frame();
	}
	
	private void refresh()
	{
		Object[][] temp =leases.display_all_leases();
		Object[][] filling = new Object[temp.length][3];
		
		for (int i=0;i<temp.length;i++)
		{
			if ((!String.valueOf(temp[i][6]).equals(null))&&(!String.valueOf(customers.find_customer_by_id(String.valueOf(temp[i][5]))).equals(null)))
			{
				if (String.valueOf(temp[i][5]).equals("false"))
				{
					filling[i]=new Object[]{temp[i][0], customers.find_customer_by_id(String.valueOf(temp[i][6]))[1], "Active"};
				}
				else
				{
					filling[i]=new Object[]{temp[i][0], customers.find_customer_by_id(String.valueOf(temp[i][6]))[1], "Completed"};
				}
			}
		}
		lease_table.setModel(new MyTableModel(filling, column_names));
	}
	
	private void make_table()
	{

		Object[][] temp =leases.display_all_leases();
		Object[][] filling = new Object[temp.length][3];
		
		for (int i=0;i<temp.length;i++)
		{
			if ((!String.valueOf(temp[i][6]).equals(null))&&(customers.is_such_customer(String.valueOf(temp[i][6])))&&(!String.valueOf(customers.find_customer_by_id(String.valueOf(temp[i][6]))).equals(null)))
			filling[i]=new Object[]{temp[i][0], customers.find_customer_by_id(String.valueOf(temp[i][6]))[1], String.valueOf(temp[i][5])};
		}
		
		lease_table = new JTable() ;

		lease_table.setFillsViewportHeight(true);
		lease_table.setModel(new MyTableModel(filling, column_names)); 
		lease_table.setPreferredScrollableViewportSize(new Dimension(400, 70));
		lease_table.setAutoCreateRowSorter(true);
		lease_table.setRowSelectionAllowed(true);
		lease_table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		lease_table.getTableHeader().setReorderingAllowed(false);
		lease_table.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
		lease_table.getSelectionModel().addListSelectionListener(new ListSelectionListener(){
	        public void valueChanged(ListSelectionEvent event) {
	            if (lease_table.getSelectedRow()!=-1)
	            {
	            	if (lease_table.getValueAt(lease_table.getSelectedRow(), 2).toString().equals("Active"))
	            	{
	            		complete.setVisible(true);
	            	}
		            else
		            {
		            	complete.setVisible(false);
		            }
	            
	            }
	        }
	    });
		DefaultTableCellRenderer right_renderer = new DefaultTableCellRenderer();
		right_renderer.setHorizontalAlignment( JLabel.RIGHT );
		lease_table.setDefaultRenderer(String.class, right_renderer);
		
		JScrollPane scroll_pane = new JScrollPane(lease_table);
		scroll_pane.setBorder(new EmptyBorder(5,10,5,10));
		getContentPane().add(scroll_pane, BorderLayout.CENTER);
}
	private void make_ctr_buttons()
	{
		JPanel ctr_btn_panel = new JPanel();
		ctr_btn_panel.setBorder(new EmptyBorder(3,3,0,3));
		
		getContentPane().add(ctr_btn_panel, BorderLayout.SOUTH);
		ctr_btn_panel.setLayout(new BorderLayout(0, 0));
		
		JPanel panel = new JPanel();
		ctr_btn_panel.add(panel, BorderLayout.NORTH);
		panel.setLayout(new BorderLayout(0, 0));
		
		JPanel panel_1 = new JPanel();
		panel.add(panel_1, BorderLayout.EAST);
		
		complete = new JButton("Complete");
		panel_1.add(complete);
		complete.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e ) { complete(); } });
		complete.setVisible(false);
		
		JButton btn_edit = new JButton("Details");
		panel_1.add(btn_edit);
		btn_edit.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { details(); } });
	}
	
	private void make_search(JPanel panel)
	{
		JPanel search_panel = new JPanel();
		panel.add(search_panel, BorderLayout.WEST);
		search_panel.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
		
		JLabel lblNewLabel = new JLabel("Reg. Number:");
		search_panel.add(lblNewLabel);
		
		text_field = new JTextField();
		search_panel.add(text_field);
		text_field.setColumns(20);
		
		JButton btn_search = new JButton("Search");
		btn_search.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { search(); } });
		search_panel.add(btn_search);
		
		JButton btnClear = new JButton("Clear");
		btnClear.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { text_field.setText(""); refresh();} });
		search_panel.add(btnClear);
		
	}
	
	private void complete()
	{
		int row_index = lease_table.getSelectedRow();
		if (row_index != -1)
		{
			if( lease_table.getModel().getValueAt(row_index, 0) != null)
			{
				LeaseFinish p = new LeaseFinish((String)lease_table.getModel().getValueAt(row_index, 0));
				getDesktopPane().add(p);
				Point current_win = getLocationOnScreen();
				int x = (int)current_win.getX() + 20;
				int y = (int) current_win.getY() + 5;
				p.setLocation(x,y);
				p.setVisible(true);
			}
		}
	}
	
	private void details()
	{
		int row_index = lease_table.getSelectedRow();
		if (row_index != -1)
		{
			String id=String.valueOf( lease_table.getModel().getValueAt(row_index, 0));
			NewLeaseGUI p = new NewLeaseGUI(leases.lease_machine_ids(id),String.valueOf(leases.display_lease_id(id)[6]),String.valueOf(leases.display_lease_id(id)[3]),String.valueOf(leases.display_lease_id(id)[2]));
			getDesktopPane().add(p);
			Point current_win = getLocationOnScreen();
			int x = (int)current_win.getX() + 20;
			int y = (int) current_win.getY() + 5;
			p.setLocation(x,y);
			p.setVisible(true);
		}
	}
	
	private void search()
	{
		Object[][] filling = new Object[1][3];
		String info = text_field.getText();
		if (leases.is_such_lease_id(info))	
		{
				Object [] temp = leases.display_lease_id(info);
				if ((!String.valueOf(temp[6]).equals(null))&&(!String.valueOf(customers.find_customer_by_id(String.valueOf(temp[5]))).equals(null)))
				{
					if (String.valueOf(temp[5]).equals("false"))
					{
						filling[0]=new Object[]{temp[0], customers.find_customer_by_id(String.valueOf(temp[6]))[1], "Active"};
					}
					else
					{
						filling[0]=new Object[]{temp[0], customers.find_customer_by_id(String.valueOf(temp[6]))[1], "Completed"};
					}
					lease_table.setModel(new MyTableModel(filling, column_names));	
				}
				else
				{
					lease_table.setModel(new MyTableModel(new Object[][]{}, column_names));	
				}
				
		}
		else
		{
			lease_table.setModel(new MyTableModel(new Object[][]{}, column_names));	
		}
	
	}
	
	private void fill_frame()
	{
		setBounds(100, 100, 395, 252);
		getContentPane().setLayout(new BorderLayout(0, 0));
		
		
		JPanel panel = new JPanel();
		getContentPane().add(panel, BorderLayout.NORTH);
		panel.setLayout(new BorderLayout(0, 0));
		
		make_search(panel);
		make_ctr_buttons();
		make_table();
	}
}